package com.hr.controller;

import com.hr.dto.Result;
import com.hr.entity.Menu;
import com.hr.service.MenuService;
import com.hr.vo.MenuVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜单管理")
@CrossOrigin
@RestController
public class MenuController {

    @Resource
    private MenuService menuService;

    @ApiOperation("新增菜单")
    @PostMapping("addMenu")
    public Result addMenu(@ApiParam(value = "菜单对象", required = true) @RequestBody Menu menu){
        menuService.addMenu(menu);
        return Result.ok();
    }

    @ApiOperation("删除菜单")
    @GetMapping("deleteMenu")
    public Result deleteMenu(@ApiParam(value = "菜单id", required = true) @RequestParam("menuId") Integer id){
        menuService.deleteMenu(id);
        return Result.ok();
    }

    @ApiOperation("更新菜单")
    @PostMapping("updateMenu")
    public Result updateMenu(@ApiParam(value = "菜单对象", required = true) @RequestBody Menu menu){
        menuService.updateMenu(menu);
        return Result.ok();
    }

    @ApiOperation("查询菜单列表")
    @GetMapping("getMenuList")
    public Result getMenuList(){
        List<Menu> menuList = menuService.getMenuList();
        return Result.ok(menuList);
    }

    @ApiOperation("查询可用的子菜单")
    @GetMapping("getEnabledChildMenuList")
    public Result getEnabledChildMenuListByParentId(@ApiParam(value = "菜单父id", required = true) @RequestParam("id") Integer id){
        List<Menu> menuList = menuService.getEnabledChildMenuList(id);
        return Result.ok(menuList);
    }

    @ApiOperation("查询菜单")
    @GetMapping("getMenu")
    public Result getMenu(@ApiParam(value = "菜单id", required = true) @RequestParam("id") Integer id){
        return Result.ok(menuService.getMenuById(id));
    }

    @ApiOperation("查询包含权限的菜单列表")
    @GetMapping("getMenuListWithAuth")
    public Result getMenuListWithAuth(@ApiParam(value = "角色id", required = true) @RequestParam("roleId") Integer roleId){
        List<MenuVO> menuListWithAuth = menuService.getMenuListWithAuthByRoleId(roleId);
        return Result.ok(menuListWithAuth);
    }

}
